package com.shg.admin.servlet.register;


import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.shg.admin.servlet.MyFilter;
import com.shg.admin.servlet.MyServlet;
import com.shg.admin.servlet.MyServletContextListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletContextListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 这个配置类用来注入原生的servlet,filter和listener
 */
@Configuration
public class MyRegisterConfig {

    @Bean
    public ServletRegistrationBean<Servlet> servletRegistrationBean() {
        ServletRegistrationBean<Servlet> servletServletRegistrationBean = new ServletRegistrationBean<>();
        MyServlet myServlet = new MyServlet();
        servletServletRegistrationBean.setServlet(myServlet);
        servletServletRegistrationBean.setUrlMappings(Collections.singletonList("/my"));
        return servletServletRegistrationBean;
    }

    // 配置Druid的监控页
    // @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet() {
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>();
        StatViewServlet statViewServlet = new StatViewServlet();
        servletRegistrationBean.setServlet(statViewServlet);
        servletRegistrationBean.setUrlMappings(Arrays.asList("/druid/*"));
        servletRegistrationBean.addInitParameter("loginUsername", "druid");
        servletRegistrationBean.addInitParameter("loginPassword", "druid");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<Filter> filterFilterRegistrationBean() {
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        MyFilter myFilter = new MyFilter();
        filterFilterRegistrationBean.setFilter(myFilter);
        filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/css/*", "/images/*", "/my"));
        return filterFilterRegistrationBean;
    }

    // WebStatFilter用于采集web-jdbc关联监控的数据。
    // @Bean
    public FilterRegistrationBean<WebStatFilter> druidWebStatFilter() {
        FilterRegistrationBean<WebStatFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        WebStatFilter webStatFilter = new WebStatFilter();
        filterFilterRegistrationBean.setFilter(webStatFilter);
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        // filterFilterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterFilterRegistrationBean.setInitParameters(initParameters);
        filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterFilterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> servletListenerRegistrationBean() {
        ServletListenerRegistrationBean<ServletContextListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
        MyServletContextListener myServletContextListener = new MyServletContextListener();
        servletListenerRegistrationBean.setListener(myServletContextListener);
        return servletListenerRegistrationBean;
    }
}
